<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class CoreViewHistory {
	var $_data;
	var $_limit;
	
	//constructor
	function CoreViewHistory() {
		$this->reset();
		$this->_limit = 10;
	}
	
	//methods
	function reset() {
		$this->_data = array();
		$this->_data['p'] = '';
		$this->_data['pc'] = '';
		$this->_data['u'] = '';
		$this->_data['sw'] = '';
	}
	
	function setLimit($limit) {
		if( is_numeric($limit) && $limit > 0 ) {
			$this->_limit = $limit;
		}
	}
	
	function getLimit() {
		return $this->_limit;
	}
	
	//
	function get($type = 'p', $count = '') {
		$as = array();
		if( isset($this->_data[$type]) ) {
			$a = explode('|', $this->_data[$type]);
			if( is_array($a) ) {
				foreach($a as $k => $v) {
					if( $v > 0 ) {
						$as[] = $v;
					}
				}
			}
			if( is_array($as) ) {
				$this->_data[$type] = implode('|', $as);
			} else {
				$this->_data[$type] = '';
			}
		}
		
		if( $count > 0 && $count < $this->_limit ) {
			return array_slice($as, 0, $count);
		} else {
			return $as;
		}
	}
	
	function add($value, $type = 'p') {
		if( isset($this->_data[$type]) ) {
			$as = array();
			$as[] = $value;
			$a = explode('|', $this->_data[$type]);
			if( is_array($a) ) {
				foreach($a as $k => $v) {
					if( $v > 0 && $v != $value ) {
						$as[] = $v;
					}
				}
			}
			if( sizeof($as) >= $this->_limit ) {
				$as = array_slice($as, 0, $this->_limit);
			}
			$this->_data[$type] = implode('|', $as);
		} else {
			$this->_data[$type] = array();
			$this->_data[$type]['0'] = $value;
		}
	}
	
	function delete($value, $type = 'p') {
		if( isset($this->_data[$type]) ) {
			$as = array();
			$a = explode('|', $this->_data[$type]);
			if( is_array($a) ) {
				foreach($a as $k => $v) {
					if( $v > 0 && $v != $value ) {
						$as[] = $v;
					}
				}
			}
			if( is_array($as) ) {
				$this->_data[$type] = implode('|', $as);
			} else {
				$this->_data[$type] = '';
			}
		}
	}
}
//
?>